package tv.youxing.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okserver.OkDownload;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.entity.AlbumInfoModel;

/**
 * Created by apple on 2017/4/22.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .adapter
 * 文件名：   AlbumVideoAdapter
 * 创建者：   叶克武
 * 创建时间： 2017/4/22 下午5:04
 * 描述：    TODO
 */
public class AlbumVideoAdapter extends BaseAdapter {

    private Context context;
    private List<AlbumInfoModel.ResultBean.MultimediaBean> albumVideoModelList = new ArrayList<>();
    private LayoutInflater layoutInflater;

    public AlbumVideoAdapter(Context context, List<AlbumInfoModel.ResultBean.MultimediaBean> albumVideoModelList) {
        this.context = context;
        this.albumVideoModelList = albumVideoModelList;
        this.layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return albumVideoModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return albumVideoModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder==null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.layout_item_album_video,null);
            viewHolder.ivHead = convertView.findViewById(R.id.head);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.tvPlayCount = (TextView) convertView.findViewById(R.id.play_count);
            viewHolder.tvPingLunCount = (TextView) convertView.findViewById(R.id.pinglun_count);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.time);
            viewHolder.tvPostTime = (TextView) convertView.findViewById(R.id.post_time);
            viewHolder.ivDownload = (ImageView) convertView.findViewById(R.id.ivDownload);
            viewHolder.tvUpdateTime = convertView.findViewById(R.id.update_time);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        AlbumInfoModel.ResultBean.MultimediaBean model = albumVideoModelList.get(position);


        ImageLoader.getInstance().displayImage(model.getCover(), viewHolder.ivHead);
        viewHolder.tvTitle.setText(model.getTitle().toString());
        viewHolder.tvPlayCount.setText("播放 "+model.getPlay_count()+"");
        viewHolder.tvPingLunCount.setText("时间 "+model.getComment_count()+"");
        viewHolder.tvTime.setText("时间 "+model.getDuration());

        viewHolder.tvUpdateTime.setText(model.getUpdated_at());

        if (OkDownload.getInstance().getTask(albumVideoModelList.get(position).getPlay_url()) != null) {
            viewHolder.ivDownload.setSelected(true);
            viewHolder.ivDownload.setEnabled(false);
        } else {
            viewHolder.ivDownload.setSelected(false);
            viewHolder.ivDownload.setEnabled(true);
        }


        return convertView;
    }

    class ViewHolder{
        private ImageView ivHead;
        private TextView tvTitle;
        private TextView tvPlayCount;//播放数
        private TextView tvPingLunCount;//评论数
        private TextView tvTime;//播放时间

        private TextView tvPostTime;//发布时间
        private ImageView ivDownload;//下载

        private TextView tvUpdateTime;//更新时间
    }


}
